package com.cmc.cloud.cmclink.doc.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.entity.AttachmentTypeDO;
import com.cmc.cloud.cmclink.doc.service.AttachmentTypeService;
import com.cmc.cloud.cmclink.doc.vo.attachmenttypevo.AttachmentTypeCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.attachmenttypevo.AttachmentTypePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.attachmenttypevo.AttachmentTypeRespVO;
import com.cmc.cloud.cmclink.doc.vo.attachmenttypevo.AttachmentTypeUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * {table.classComment}前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 附件类型")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/atType")
public class AttachmentTypeController {

    @Resource
    private AttachmentTypeService attachmentTypeService;

    @Operation(summary = "创建附件类型")
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission({'doc:mantainAttachmentType:add','doc:attachment:add'})")
    public CommonResult<Void> createAttachmentType(@Valid @RequestBody AttachmentTypeCreateReqVO createReqVO) {
        attachmentTypeService.createAttachmentType(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新附件类型")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission({'doc:mantainAttachmentType:edit','doc:attachment:edit'})")
    public CommonResult<Void> updateAttachmentType(@Valid @RequestBody AttachmentTypeUpdateReqVO updateReqVO) {
        attachmentTypeService.updateAttachmentType(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新附件类型是否存在长期保函")
    @PostMapping("/update/valid")
    public CommonResult<Boolean> updateValidAttachmentType(@Valid @RequestBody AttachmentTypeUpdateReqVO updateReqVO) {
        Boolean check = attachmentTypeService.updateValidAttachmentType(updateReqVO);
        return CommonResult.success(check);
    }

    @Operation(summary = "删除附件类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:mantainAttachmentType:delete','doc:attachment:delete'})")
    public CommonResult<Void> deleteAttachmentType(@PathVariable("id") Long id) {
        attachmentTypeService.deleteAttachmentType(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得附件类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @GetMapping("/getById/{id}")
    public CommonResult<AttachmentTypeRespVO> getAttachmentType(@PathVariable("id") Long id) {
        AttachmentTypeDO attachmentType = attachmentTypeService.getAttachmentType(id);
        return CommonResult.success(BeanUtil.copyProperties(attachmentType, AttachmentTypeRespVO.class));
    }

    @Operation(summary = "获得附件类型列表")
    @GetMapping("/listAll")
    public CommonResult<List<AttachmentTypeRespVO>>
        getAttachmentTypeList(@RequestParam(value = "keyWord", required = false) String keyWord) {
        List<AttachmentTypeDO> list = attachmentTypeService.getAttachmentTypeList(keyWord);
        return CommonResult.success(BeanUtil.copyToList(list, AttachmentTypeRespVO.class));
    }

    @Operation(summary = "获得附件类型列表")
    @GetMapping("/listAllSpc")
    public CommonResult<List<AttachmentTypeRespVO>>
        listAllSpc(@RequestParam(value = "keyWord", required = false) String keyWord) {
        List<AttachmentTypeDO> list = attachmentTypeService.getAttachmentTypeSpcList(keyWord);
        return CommonResult.success(BeanUtil.copyToList(list, AttachmentTypeRespVO.class));
    }

    @Operation(summary = "获得附件类型分页")
    @PostMapping("/getPage")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<AttachmentTypeRespVO>>
        getAttachmentTypePage(@Valid @RequestBody AttachmentTypePageReqVO pageVO) {
        PageResult<AttachmentTypeDO> pageResult = attachmentTypeService.getAttachmentTypePage(pageVO);
        List<AttachmentTypeRespVO> results = BeanUtil.copyToList(pageResult.getList(), AttachmentTypeRespVO.class);
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "特种货查询必传文件类型")
    @GetMapping("/getRequiredAttachList")
    public CommonResult<List<AttachmentTypeRespVO>>
        getRequiredAttachList(@RequestParam(value = "applyType") String applyType) {
        List<AttachmentTypeDO> list = attachmentTypeService.getRequiredAttachList(applyType);
        return CommonResult.success(BeanUtil.copyToList(list, AttachmentTypeRespVO.class));
    }

}
